package com.aj.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.aj.http.AsynchronizationHttpRequest.IHttpCallBack;
import com.aj.http.request.ForgetPasswordRequest;
import com.aj.http.response.BaseRespond;
import com.aj.http.response.Common;
import com.aj.utils.Contants;
import com.aj.utils.MobileUtil;

public class SetPasswordActivity extends BaseActivity {
	private Button ok;
	private String pageFlag;
	private String phoneString;
	private String verificationString;
	private String verificationToken;
	private EditText password_enter;
	private EditText repassword_enter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_set_password);
		initView();
	}

	private void initView() {
		ok = (Button) findViewById(R.id.ok);
		password_enter = (EditText) findViewById(R.id.password_enter);
		repassword_enter = (EditText) findViewById(R.id.repassword_enter);
		if (getIntent().hasExtra(Contants.PHONEFLAG_STRING)) {
			phoneString = getIntent().getStringExtra(Contants.PHONEFLAG_STRING);
		}
		if (getIntent().hasExtra(Contants.VERIFICATIONFLAG_STRING)) {
			verificationString = getIntent().getStringExtra(
					Contants.VERIFICATIONFLAG_STRING);
		}

		if (getIntent().hasExtra(Contants.SMSVALIDATETOKEN_STRING)) {
			verificationToken = getIntent().getStringExtra(
					Contants.SMSVALIDATETOKEN_STRING);
		}
		setActionBarTitle(R.string.setpassword_title_text);
		pageFlag = getIntent().getStringExtra(Contants.PAGEFLAG_STRING);
		ClickEvent clickEvent = new ClickEvent();
		ok.setOnClickListener(clickEvent);
	}

	class ClickEvent implements OnClickListener {
		@Override
		public void onClick(View v) {
			Intent intent = null;
			switch (v.getId()) {
			case R.id.ok:
				if (password_enter.getText().toString().length() < 6
						|| repassword_enter.getText().toString().length() < 6) {
					Toast.makeText(
							SetPasswordActivity.this,
							getString(R.string.password_length_error_info_text),
							Toast.LENGTH_LONG).show();
					return;
				}
				if (!password_enter.getText().toString()
						.equals(repassword_enter.getText().toString())) {
					Toast.makeText(SetPasswordActivity.this,
							getString(R.string.password_sure_error_info_text),
							Toast.LENGTH_LONG).show();
					return;
				}
				showProgressDialog();
				ForgetPasswordRequest request = new ForgetPasswordRequest();
				request.excute(SetPasswordActivity.this, password_enter
						.getText().toString(), verificationToken, MobileUtil
						.getImei(SetPasswordActivity.this),
						new IHttpCallBack() {

							@Override
							public void success(String result) {
								try {
									dimissProgressDialog();
									Common common = new Common(new JSONObject(
											result));
									if (common != null) {
										if (common
												.getReturnCode()
												.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
											if (pageFlag != null
													&& pageFlag
															.equals(Contants.REGISTER_PAGEFLAG_STRING)) {
												if (LoginActivity.instance != null) {
													LoginActivity.instance
															.finish();
													LoginActivity.instance = null;
												}
											}
											if (EnterPhoneActivity.instance != null) {
												EnterPhoneActivity.instance
														.finish();
												EnterPhoneActivity.instance = null;
											}
											if (EnterVerificationCodeActivity.instance != null) {
												EnterVerificationCodeActivity.instance
														.finish();
												EnterVerificationCodeActivity.instance = null;
											}
											Toast.makeText(
													SetPasswordActivity.this,
													getString(R.string.register_success_text),
													Toast.LENGTH_LONG).show();
											finish();
										} else {
											Toast.makeText(
													SetPasswordActivity.this,
													common.getErrorMsg(),
													Toast.LENGTH_LONG).show();
										}
									}
								} catch (JSONException e) {
									e.printStackTrace();
								}
							}

							@Override
							public void fail(String result) {
								dimissProgressDialog();
								try {
									dimissProgressDialog();
									Common common = new Common(new JSONObject(
											result));
									if (common != null) {
										Toast.makeText(
												SetPasswordActivity.this,
												common.getErrorMsg(),
												Toast.LENGTH_LONG).show();
									}
								} catch (JSONException e) {
									e.printStackTrace();
								}
							}
						});
				break;
			default:
				break;
			}
		}
	}
}
